package com.ctz.commonframe.ui.view.starView;

import java.util.ArrayList;
import java.util.List;

import com.ctz.commonframe.ui.view.starView.StarLinearLayoutLogic.IStarSelectedLis;

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

/**
 * 评分星星控件
 * 
 */
public class StarLinearLayout extends LinearLayout {
	private Context mContext;
	private StarLayoutParams mParams;
	private List<ImageView> stars;
	private StarLinearLayoutLogic logic;
	private OnStarChangeListener onStarChangeListener;//监听星星变化接口

	public StarLinearLayout(Context context) {
		this(context, null);
	}

	public StarLinearLayout(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
	}

	@TargetApi(Build.VERSION_CODES.HONEYCOMB)
	public StarLinearLayout(Context context, AttributeSet attrs,
			int defStyleAttr) {
		super(context, attrs, defStyleAttr);
		mContext = context;
		init();
	}

	private void init() {
		logic = new StarLinearLayoutLogic(getStarSelectedLis());
		stars = new ArrayList<ImageView>();
	}

	public void setStarParams(StarLayoutParams params) {
		mParams = params;
		logic.setParams(params);

		updateStars();
		invalidate();
	}

	private void updateStars() {
		if (mParams == null)
			return;
		if (stars != null) {
			stars.clear();
			removeAllViews();
		}
		int newStarNum = mParams.isSelectable() ? mParams.getTotalStarNum()
				: mParams.getSelectedStarNum();
		for (int i = 0; i < newStarNum; i++) {
			ImageView imageView = createStar(i);
			stars.add(imageView);
			addView(imageView);
		}
	}

	/**
	 * 创建星星imageview  
	 * @param pos
	 * @return
	 */
	private ImageView createStar(final int pos) {
		ImageView imageView = new ImageView(mContext);
		DrawableTools.setImageDrawableWithStateList(imageView,
				new DrawableAttrs(mContext).setNormalDrawable(mParams.getNormalStar())
						.setSelectedDrawable(mParams.getSelectedStar()));
		if (mParams.isSelectable()) { // 可点击
			imageView.setEnabled(true);
			if (pos < mParams.getSelectedStarNum()) { // 已选中的星星
				imageView.setSelected(true);
			} else { // 未被选中的星星
				imageView.setSelected(false);
			}
		} else { // 不可点击，仅展示
			imageView.setEnabled(false);
			imageView.setSelected(true);
		}
		imageView.setPadding(mParams.getStarHorizontalSpace() / 2, 0,
				mParams.getStarHorizontalSpace() / 2, 0);
		imageView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				logic.onStarClick(pos);
				for (int i = 0; i < mParams.getTotalStarNum(); i++) {
					if(pos==i){
						int decade=10-mParams.getTotalStarNum();//十位
						int start=(decade+i)*10;//分段起始值
						StarLinearLayout.this.onStarChangeListener.onStarChange(start);  //调用监听接口
					}
				}
			}
		});
		return imageView;
	}

	private IStarSelectedLis getStarSelectedLis() {
		return new IStarSelectedLis() {

			@Override
			public void onSelected(int curStarNum) {
				for (int i = 0; i < mParams.getTotalStarNum(); i++) {
					if (i < curStarNum) {
						stars.get(i).setSelected(true);
					} else {
						stars.get(i).setSelected(false);
					}
				}
			}
		};
	}

	public StarLinearLayoutLogic getLogic() {
		return logic;
	}
	
	 /**
     * 定义星星点击的监听接口
     */
    public interface OnStarChangeListener {
        void onStarChange(int start);
    }

    /**
     * 设置监听
     * @param onStarChangeListener
     */
    public void setOnStarChangeListener(OnStarChangeListener onStarChangeListener){
        this.onStarChangeListener = onStarChangeListener;
    }


}
